<?php
namespace Home\Controller;

use Think\Controller;

class UserController extends Controller
{
    /*
     *添加新的地址
     */
    public function addAddress()
    {
        $name          = I('post.name');
        $phone         = I('post.phone');
        $city          = I('post.city');
        $cityCode      = I('post.citycode');
        $selectThis    = I('post.selectThis');
        $addressName   = I('post.addressName');
        $addressDetail = I('post.addressDetail');
        $lng           = I('post.lng');
        $lat           = I('post.lat');
        $condition     = [
            'name'           => $name,
            'phone'          => $phone,
            'city'           => $city,
            'city_code'      => $cityCode,
            'address_name'   => $addAddress,
            'type'           => $selectThis,
            'address_detail' => $addressDetail,
            'lng'            => $lng,
            'lat'            => $lat,
        ];

        $address = M('address');
        $result  = $address
            ->create($condition)
            ->add();
        if ($result) {
            success($data);
        } else {
            fail(001, '添加失败！');
        }
    }

    /**
     * 我的地址
     */
    public function addressList()
    {
        try {
            $where = [
                'user_id' => USER_ID,
            ];
            $address = M('address');
            $list    = $address
                ->where($where)
                ->filed('id,
					name,
					phone,
					default,
					address,
					lng,
					lat')
                ->order('default desc')
                ->select();
            success(['data' => $list]);
        } catch (Exception $e) {
            fail(101, '服务器异常！');
        }
    }

    /**
     * 申请加盟合伙人
     */
    public function apply()
    {
        $shop_name = I('post.shopname');
        $user_name = I('post.username');
        $phone     = I('post.phone', '');
        $address   = I('post.address');
        $city_code = I('post.citycode');
        $city_name = I('post.city_name');
        // $files = I('post.file');
        $condition = [
            'shop_name' => $shop_name,
            'user_name' => $user_name,
            'phone'     => $phone,
            'address'   => $address,
            'city_code' => $city_code,
            'city_name' => $city_name,

        ];

        $apply = M('partner')
            ->create($condition)
            ->add();
        if ($apply) {
            success(['data' => $data]);
        } else {
            fail(101, '加盟失敗！');
        }
    }

    /**
     * 兑换红包
     */
    public function exchange()
    {
        $user_id    = USER_ID;
        $cdkey      = I('post.exchange', '');
        $shop_cdkey = M('shop_cdkey')
            ->where(['cdkey' => $cdkey])
            ->field('status')
            ->find();
        if (!empty($shop_cdkey)) {
            if ($shop_cdkey) {
                if ($cdkey) {
                    $condition = [
                        'user_id' => $user_id,
                        'cdkey'   => $cdkey,
                        // 'price'   =>  ?  ? ,
                        'time'    => date('Y-m-d H:i:s'),
                    ];

                    $exchange = M('cdkey')
                        ->data($condition)
                        ->add();
                    if ($exchange) {
                        success(['data' => $data]);
                    }
                } else {
                    fail(101, '缺少兑换码！');
                }
            } else {
                fail(102, '该兑换码已过期！');
            }
        } else {
            fail(101, '该兑换码不存在！');
        }
    }

    /**
     * 用户优惠券
     */
    public function couponList()
    {
        $openid     = I(post . openid, '123');
        $num = 10;
        $where = [
        	'openid' => $openid,
        ];
        $user = M('user');
        $couponList = $user
        	->alias('a')
        	->where($where)
        	->join('space_couponList b on b.user_id = a.user_id','left')
        	->limit($num)
        	->field('b.id,
        			b.title,
        			b.status,
        			b.sprice,
        			b.photo,
        			b.start_time,
        			b.end_time')
        	->select();
        success(['data' => $couponList]);
       
    }

    /**
     * 用户收藏列表
     */
    public function favourite()
    {
    	$user_id = I('post.user_id','1');
    	$where = ['user_id' => $user_id];
    	$favourite = M('favourite')
    		->where($where)
    		->alias('a')
    		->join('space_type b on b.id = a.type_id','left')
    		->field('a.	id,
    				a.avatar,
    				a.square,
    				a.price,
    				a.stage,
    				a.district,
    				b.type')
    		->order('id')
    		->page('1,10')
    		->select();
    	success(['data' => $favourite]);
    }

}
